package com.junci.mtest.views;

import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;


import com.junci.mtest.model.bean.DefaultBean;
import com.junci.mtest.utils.MyDiffUtilCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YM
 * Create Date 2024/4/30 19:05
 * Used
 */
public abstract class BaseAdapterHelper {
    private List<DefaultBean> mList = new ArrayList<>();
    private RecyclerView.Adapter adapter;

    // diff 更新方式
    public void diffUpdate(final List<DefaultBean> newCoderList){
        final MyDiffUtilCallback diffUtilCallback = new MyDiffUtilCallback(this.mList, newCoderList);
        // 获取差异结果（注意这里是耗时操作，如果数据量大的时候需要放到后台线程处理差异，否则会阻塞主线程）
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffUtilCallback);
        cloneData(newCoderList);
        // DiffResult 再把差异分发给 Adapter，adapter 最后根据接收到的差异数据做更新
        diffResult.dispatchUpdatesTo(adapter);
    }
    private void cloneData(List<DefaultBean> newCoderList) {
        this.mList.clear();
        this.mList.addAll(newCoderList);
    }

    public List<DefaultBean> getmList() {
        return mList;
    }

    public void setmList(List<DefaultBean> mList) {
        this.mList = mList;
    }

    public RecyclerView.Adapter getAdapter() {
        return adapter;
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        this.adapter = adapter;
    }
}
